﻿using Memorandum.DBContext;
using Memorandum.Models;
using Memorandum.Service;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace Memorandum
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        //public  const string  mbox= "test11";
        private DispatcherTimer timer = null;
        protected override void OnStartup(StartupEventArgs e)
        {
           
            //timer = new DispatcherTimer();
            //timer.Tick += new EventHandler(dispatcherTimer_Tick);
            //timer.Interval = new TimeSpan(0, 0, 10);
            //timer.Start();
        }
        private List<int> meetClockId = new List<int>();
        public static readonly object objlock = new object();
        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            Task.Run(() =>
            {
                lock(objlock)
                {

                    var items = SqliteDBContext.Instance.RemindItems;
                    foreach (var item in items)
                    {

                        var TotalMinutes = (int)item.AlarmTime.TimeOfDay.TotalMinutes;
                        var TotalMinutes2 = (int)DateTime.Now.TimeOfDay.TotalMinutes;

                        if (meetClockId.Contains(item.ID))
                        {
                            if (TotalMinutes2 - TotalMinutes > 1)
                            {
                                meetClockId.Remove(item.ID);
                            }
                            continue;
                        }

                        if (item.type == RemindType.Daily)
                        {
                            if (TotalMinutes == TotalMinutes2)
                            {
                                meetClockId.Add(item.ID);
                                Application.Current.Dispatcher.Invoke(() =>
                                {
                                    MessageManager.Instance.show(item.Title, item.Content, item.dateTime);
                                });
                            }
                        }
                        else
                        {
                            //var TotalMinutes = (int)item.AlarmTime.TimeOfDay.TotalMinutes;
                            //var TotalMinutes2 = (int)DateTime.Now.TimeOfDay.TotalMinutes;
                            if ((int)item.dateTime.Year == (int)DateTime.Now.Year && (int)item.dateTime.Month == (int)DateTime.Now.Month
                                && (int)item.dateTime.Day == (int)DateTime.Now.Day && TotalMinutes == TotalMinutes2)
                            {
                                meetClockId.Add(item.ID);
                                Application.Current.Dispatcher.Invoke(() =>
                                {
                                    MessageManager.Instance.show(item.Title, item.Content, item.dateTime);
                                });
                            }
                        }
                    }
                }

            });
        }
    }
}
